package com.chengxiaoxiao.commons;

public class PageUtils {

    /**
     * 展示导航
     * @param page
     * @return
     */
    public static String ShowPageNavigate(Page page) {
        String redirectTo = page.getUrl();
        int pageSize = page.getPageSize();
        int totalPages = page.getTotalPageNum();
        int currentPage = page.getPageNum();

        StringBuilder output = new StringBuilder();
        if (totalPages >= 1) {
            if (currentPage != 1) {//处理首页连接
                output.append("<a class='pageLink' href='" + redirectTo + "&num=1'>首页</a> ");
            }
            if (currentPage > 1) {//处理上一页的连接
                output.append("<a class='pageLink' href='" + redirectTo + "&num=" + page.getPrePageNum() + "'>上一页</a> ");
            } else {
                output.append("<span class='pageLink'>上一页</span>");
            }

            output.append(" ");
            int currint = 5;
            for (int i = 0; i <= 10; i++) {//一共最多显示10个页码，前面5个，后面5个
                if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages) {
                    if (currint == i) {//当前页处理
                        //output.Append(string.Format("[{0}]", currentPage));
                        output.append("<a class='cpb' href='" + page.getUrl() + "&num=" + page.getPageNum() + "'>" + page.getPageNum() + "</a> ");
                    } else {//一般页处理
                        int nums =currentPage + i - currint;
                        output.append("<a class='pageLink' href='" + page.getUrl() + "&num=" + nums+ "'>" + nums + "</a> ");
                    }
                }
                output.append(" ");
            }
            if (currentPage < totalPages) {//处理下一页的链接
                output.append("<a class='pageLink' href='" + page.getUrl() + "&num=" + page.getNextPageNum() + "'>下一页</a> ");
            } else {
                //output.Append("<span class='pageLink'>下一页</span>");
            }
            output.append(" ");
            if (currentPage != totalPages) {
                output.append("<a class='pageLink' href='" + page.getUrl() + "&num=" + page.getTotalPageNum() + "'>末页</a> ");
            }
            output.append(" ");
        }
        output.append("第" + page.getPageNum() + "页 / 共" + page.getTotalPageNum() + "页");//这个统计加不加都行

        return output.toString();
    }
}
